package com.younion.action.manage;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.younion.business.ICustomerService;
import com.younion.business.IPermissionsService;
import com.younion.business.IRolesService;
import com.younion.common.base.BaseAction;
import com.younion.common.constants.RoleType;
import com.younion.common.paginator.Paginator;
import com.younion.permission.NodePermission;
import com.younion.permission.PermissionUtil;
import com.younion.vo.RolesVO;

import cpcns.jhsks.util.MapCollectionUtils;

/**
 * @author seagull
*/
public class RolesAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	protected static final Log logger = LogFactory.getLog(RolesAction.class);

	private ICustomerService customerService = null;
	
	private IRolesService rolesService = null;
	
	private IPermissionsService permissionsService = null;
	
	private List<RoleType> roleTypes = Arrays.asList(RoleType.values());

	private RolesVO rolesVO = new RolesVO();
	
	private List<RolesVO> rolesList;// 结果集列表
	private String permission;
	
	
	/**
	 * 列表：翻页区；数据区	 
	 */
	@SuppressWarnings("unchecked")
	@RequiresPermissions("listRoles")
	public String listRoles() {
		try {
			Map paramMap = null;
			if (rolesVO != null) {
				paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(rolesVO));
				if(getSessionUser().getCustomer() != null){
					paramMap.put("customerId", getSessionUser().getCustomer().getId());
				}
				String role = NodePermission.R.getName() + PermissionUtil.colon + RoleType.SHOP.getName() + PermissionUtil.colon + getSessionUser().getRole();
				if(SecurityUtils.getSubject().hasRole(role)){
					paramMap.put("roleType", RoleType.SHOP.getName());
				}
			}
			// 获取总记录数
			int total = rolesService.getCountByMap(paramMap);
			// 根据总记录数获取分页对象
			Paginator paginator = this.getPaginator(total);
			rolesList = rolesService.get(paramMap, paginator);
			// 保存分页对象
			this.savaPaginator(paginator);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return SUCCESS;
	}

	/**
	 * 新增
	 */
	@RequiresPermissions("addRoles")
	public String addRoles() {
		return SUCCESS;
	}

	/**
	 * 修改保存，调度Jsp
	 */
	@RequiresPermissions("editRoles")
	public String editRoles() {
		try {
			rolesVO = rolesService.getById(rolesVO.getId().toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 保存动作
	 */
	@RequiresPermissions("saveRoles")
	public String saveRoles() {
		try {
			if(permission != null && !"".equals(permission))
				rolesVO.setPermissions(permission.substring(0, permission.length()-1));
			rolesVO = rolesService.save(rolesVO);
			String id = rolesVO.getId() == null ? "" : rolesVO.getId().toString();
			String log = "".equals(id) ? "添加角色" : "更新角色";
			Integer type = "".equals(id) ? 3 : 4;
			this.addLog(type, log+id, log,getSessionUser());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		request.setAttribute("refreshNavTabId", "rolesList");
		return SUCCESS;
	}

	/**
	 * 删除
	 */
	@RequiresPermissions("deleteRoles")
	public String deleteRoles() {
		try {
			String pkId = request.getParameter("ids");
			rolesService.remove(Arrays.asList(pkId.split(",")));
			this.addLog(4, "删除角色："+pkId, "删除角色",getSessionUser());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		request.setAttribute("refreshNavTabId", "rolesList");
		return SUCCESS;
	}
	/**
	 * 查看
	 */
	@RequiresPermissions("viewRoles")
	public String viewRoles() {
		try {
			String pkId = request.getParameter("ids"); 
			rolesVO = rolesService.getById(pkId);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;

	}
	
	
	public RolesVO getRolesVO() {
		return rolesVO;
	}

	public void setRolesVO(RolesVO rolesVO) {
		this.rolesVO = rolesVO;
	}

	public void setRolesService(IRolesService rolesService) {
		this.rolesService = rolesService;
	}

	public List<RolesVO> getRolesList() {
		return rolesList;
	}

	public void setRolesList(List<RolesVO> rolesList) {
		this.rolesList = rolesList;
	}

	public IPermissionsService getPermissionsService() {
		return permissionsService;
	}

	public void setPermissionsService(IPermissionsService permissionsService) {
		this.permissionsService = permissionsService;
	}

	public IRolesService getRolesService() {
		return rolesService;
	}


	public String getPermission() {
		return permission;
	}

	public void setPermission(String permission) {
		this.permission = permission;
	}

	public ICustomerService getCustomerService() {
		return customerService;
	}

	public void setCustomerService(ICustomerService customerService) {
		this.customerService = customerService;
	}

	public List<RoleType> getRoleTypes() {
		return roleTypes;
	}

	public void setRoleTypes(List<RoleType> roleTypes) {
		this.roleTypes = roleTypes;
	}
	
	
}